package cn.iocoder.yudao.module.system.service.erp.warehouseinventory;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventory.vo.WarehouseInventoryPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.warehouseinventory.vo.WarehouseInventorySaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.warehouseinventory.WarehouseInventoryDO;
import cn.iocoder.yudao.module.system.dal.mysql.erp.warehouseinventory.WarehouseInventoryMapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.WAREHOUSE_INVENTORY_NOT_EXISTS;

/**
 * 仓库库存 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarehouseInventoryServiceImpl implements WarehouseInventoryService {

    @Resource
    private WarehouseInventoryMapper warehouseInventoryMapper;

    @Override
    public Long createWarehouseInventory(WarehouseInventorySaveReqVO createReqVO) {
        // 插入
        WarehouseInventoryDO warehouseInventory = BeanUtils.toBean(createReqVO, WarehouseInventoryDO.class);
        warehouseInventoryMapper.insert(warehouseInventory);
        // 返回
        return warehouseInventory.getId();
    }

    @Override
    public void updateWarehouseInventory(WarehouseInventorySaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseInventoryExists(updateReqVO.getId());
        // 更新
        WarehouseInventoryDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseInventoryDO.class);
        warehouseInventoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseInventory(Long id) {
        // 校验存在
        validateWarehouseInventoryExists(id);
        // 删除
        warehouseInventoryMapper.deleteById(id);
    }

    private void validateWarehouseInventoryExists(Long id) {
        if (warehouseInventoryMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_INVENTORY_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseInventoryDO getWarehouseInventory(Long id) {
        return warehouseInventoryMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseInventoryDO> getWarehouseInventoryPage(WarehouseInventoryPageReqVO pageReqVO) {
        return warehouseInventoryMapper.selectPage(pageReqVO);
    }

    @Override
    public WarehouseInventoryDO getWarehouseInventory(Long warehouseId, Long customerId) {
    return warehouseInventoryMapper.selectOne(new LambdaQueryWrapper<WarehouseInventoryDO>().
    eq(WarehouseInventoryDO::getWarehouseId,warehouseId).eq(WarehouseInventoryDO::getCustomerId,customerId));
    }

    @Override
    public WarehouseInventoryDO getOrCrateWarehouseInventory(Long warehouseId, Long customerId) {
        WarehouseInventoryDO warehouseInventoryDO = warehouseInventoryMapper.selectOne(new LambdaQueryWrapper<WarehouseInventoryDO>().
                eq(WarehouseInventoryDO::getWarehouseId, warehouseId).eq(WarehouseInventoryDO::getCustomerId, customerId));
        if(ObjectUtil.isNull(warehouseInventoryDO)){
            WarehouseInventoryDO addItem=new WarehouseInventoryDO();
            addItem.setWarehouseId(warehouseId);
            addItem.setCustomerId(customerId);
            warehouseInventoryMapper.insert(addItem);
            warehouseInventoryDO=addItem;
        }
        return warehouseInventoryDO;
    }

}